package com.sun.myproject.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.sun.myproject.bean.Book;
import com.sun.myproject.dao.BookDao;
import com.sun.myproject.daoimpl.BookDaoImpl;
import com.sun.myproject.tool.ExcelUtil;

/**
 * Servlet implementation class ImportExcelServlet
 */
@WebServlet("/ImportExcelServlet")
@MultipartConfig
public class ImportExcelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ImportExcelServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 写入文件
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		System.out.println("----===-==-=-=来了");

		PrintWriter out = response.getWriter();
		// 创建目录
		File path = new File("c:\\img");
		if (!path.exists()) {
			path.mkdir();
		}
		// 获取文件
		Part img = request.getPart("excelpath");
		// 文件名

		String header = img.getHeader("content-disposition");
		System.out.println(header);
		// 获取文件名
		String fileName = getFileName(header);
		System.out.println("-------" + fileName);
		// 文件全路径
		String filePath = path.getPath() + File.separator + fileName;
		System.out.println(filePath);
		// 写入文件
		img.write(filePath);
		FileInputStream inputStream = new FileInputStream(filePath);
		ExcelUtil excelUtil = new ExcelUtil();
		try {
			List<Object> list = excelUtil.ExcelUtil(inputStream);
			String bookName = "";
			String publish = "";
			String author = "";
			int bookCount = 0;
			String bookType = "";
			String Img = "";
			System.out.println(list.toString());

			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.size());
				for (int j = 0; j <= 5; j++) {
					System.out.println(j);
					switch (j) {
					
					case 0:
						
						bookName = list.remove(0).toString();
						System.out.println(bookName);
						break;
					case 1:
						publish = list.remove(0).toString();
						System.out.println(publish);
						break;
					case 2:
						author = list.remove(0).toString();
						System.out.println(author);
						break;
					case 3:
						bookCount = Integer.parseInt(list.remove(0).toString());
						System.out.println(bookCount);
						break;
					case 4:
						bookType = list.remove(0).toString();
						System.out.println(bookType);
						break;
					case 5:
						Img = list.remove(0).toString();
						System.out.println(Img);
						break;

					}
				}
				Book book = new Book(0, bookName, publish, bookCount, author, bookType, Img);
				BookDao service = new BookDaoImpl();
				service.insert(book);
				System.out.println("成功一条");

			}
			request.getRequestDispatcher("BookListServlet").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String getFileName(String header) {

		String[] tempArr1 = header.split(";");

		String[] tempArr2 = tempArr1[2].split("=");

		String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
		return fileName;
	}
}
